package com.example.validate;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.Version;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.appserv.jdbc.DataSource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 * Servlet implementation class StartPageServlet
 */
@WebServlet("/startPage")
public class StartPageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StartPageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// Preprocess request: we actually don't need to do any business stuff, so just display JSP.
        request.getRequestDispatcher("/schools.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// Postprocess request: validate school country with countries in the database, and then go to schools_review if OK.

        // Get and validate country.
        String country = request.getParameter("country");
        
        if (checkCountry(country)){
        	request.getRequestDispatcher("/schools.jsp").forward(request, response);        	
        }else {
        	request.getRequestDispatcher("/index.jsp").forward(request, response);
        }

	}
	
	protected boolean checkCountry(String country){
		Connection con = null;
		PreparedStatement pst = null;
        ResultSet rs = null;
        DataSource ds = null;
        InitialContext ctx = null;

        String url = "jdbc:mysql:lut2";

        try {
        	ctx = new InitialContext();
        	ds = (DataSource) ctx.lookup("jdbc/lut2");
        	con = ds.getConnection();
        	pst = con.prepareStatement("SELECT full_name FROM country");
            rs = pst.executeQuery();

            while (rs.next()) {
            	String c = rs.getString(1);
            	System.out.println(c);
            	if(c.equals(country)) return true;
            }

        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Version.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(Version.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }	
		return false;
	}

}
